package com.lkm.app_ui;

import android.Manifest;
import android.app.ActivityManager;
import android.content.Context;
import android.os.Build;
import android.os.Handler;
import android.os.Looper;

import com.lkm.app_ui.m.Imple.initapp.MInitAppTask;
import com.lkm.comlib.AppBridge;
import com.lkm.comlib.AppBridgeBase;
import com.lkm.comlib.Config;
import com.lkm.comlib.MyApplicationL;
import com.lkm.comlib.down.AsyncDownloader;
import com.lkm.comlib.module.PermissionHandleAdapterImpl;
import com.lkm.frame.app.CheckAppConst;
import com.lkm.frame.app.MainApp;
import com.lkm.frame.file.FileSystem;
import com.lkm.frame.module.ModuleManager;
import com.lkm.frame.module.PermissionCondition;
import com.lkm.frame.net.Download;
import com.lkm.frame.net.imageload.AsyncImageLoaderLs;
import com.lkm.frame.task2.TaskManager;
import com.lkm.frame.task2.TaskManagerImple;
import com.lkm.java_okhttp.OkHttpDownload;
import com.mj.hx.yl.push.PushManage;

import java.util.Locale;
import java.util.TimeZone;
import java.util.concurrent.Executor;

public class MyApplication extends MyApplicationL {


    @Override
    public void onCreate() {


        if((getPackageName()+":channel").equals
                (getCurrentProcessName(this))){
            //请保证 推送进程会触发到初始化。见：https://help.aliyun.com/knowledge_detail/40002.html?spm=a2c4g.11186623.2.14.39367ff1MnBMpN#h2-- 已经接入成功，突然出现异常
            PushManage.initCloudChannel(this);
        }

        if (!isMainProcess(getApplicationContext())) {
            return;
        }
        super.onCreate();

    }

    @Override
    protected void init() {
        super.init();
        AppConst.mainHandler = new Handler(Looper.getMainLooper());

        
        AsyncDownloader.setInstance(this, new AsyncDownloader(this){
        });
        MainApp.main(initAppBaseConst(), new MInitAppTask(),new PermissionCondition() {
            @Override
            public String[] initMissPermission() {
                return new String[]{
//              鑫奥康需要用到的 ，但是在正常的手机上会没这个权限         Manifest.permission.WRITE_SETTINGS,
                        android.Manifest.permission.WRITE_EXTERNAL_STORAGE,
                        Manifest.permission.READ_PHONE_STATE
                };
            }
            @Override
            public String[] initRunPermission() {
                return new String[]{
//           鑫奥康需要用到的  ，但是在正常的手机上会没这个权限           Manifest.permission.WRITE_SETTINGS,
                        Manifest.permission.WRITE_EXTERNAL_STORAGE
                };
            }
            @Override
            public boolean isLazyRequestPermission() {
                return false;
            }
        });
    }

    public CheckAppConst initAppBaseConst() {

        
        TimeZone.setDefault(TimeZone.getTimeZone("GMT+8"));

        
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.N) {
            Locale.setDefault(Locale.Category.FORMAT, Locale.CHINESE);
        } else {
            Locale.setDefault(Locale.CHINESE);
        }


        Executor executor = Config.getTaskExecutor();
        TaskManager taskManager = new TaskManagerImple();
        ModuleManager moduleManager = new ModuleManager(new PermissionHandleAdapterImpl(application), taskManager, new Handler(Looper.getMainLooper()));


        AppConst.THREAD_POOL_EXECUTOR = executor;
        AppConst.taskManager = taskManager;
        AppConst.moduleManager = moduleManager;

        AppConst.application = application;

        return new CheckAppConst(executor, taskManager, moduleManager);
    }


    public ModuleManager getModuleManager() {
        return AppConst.getModuleManager();
    }

    public TaskManager getTaskManager2() {
        return AppConst.getTaskManager2();
    }

    @Override
    public boolean isDebug() {
        return com.lkm.app_ui.BuildConfig.DEBUG;
    }


    public static MyApplication getInstance(Context c) {
        return (MyApplication) c.getApplicationContext();
    }

    public static MyApplication getInstance() {
        return (MyApplication) application;
    }


    
    public AsyncImageLoaderLs createAsyncImageLoaderLs() {
        return new AsyncImageLoaderLs(getApplicationContext(), new OkHttpDownload(1024*2, true) {

            @Override
            protected void onBuilder(com.squareup.okhttp.Request.Builder builder) {
                super.onBuilder(builder);

//                String Authorization = MyApplication.this.getUserInfoCache().getAuthorization(MyApplication.this);





            }
        }, Config.getImageDownExecutor(), FileSystem.FileLocal.ExternalCacheDir, "imgs");
    }


    @Override
    public AppBridge getAppBridge() {
        return new AppBridge() {

            @Override
            public void goLaunchActivity(Context c) {
                
            }

            @Override
            public String upError(Context c, String app_name, String app_version, String error) {
                return null;
            }
        };
    }


    @Override
    public String getSDRootDir() {
        return "ast-market";
    }

    @Override
    public int getPhoneMinMemorySize() {
        return 0;
    }

    private AppBridgeBase mAppBridgeBase = new AppBridgeBase() {
        @Override
        public AsyncImageLoaderLs createAsyncImageLoaderLs() {
            return MyApplication.this.createAsyncImageLoaderLs();
        }

        @Override
        public Executor getTaskExecutor() {
            return Config.getTaskExecutor();
        }
    };

    @Override
    public AppBridgeBase getAppBridgeBaseInstance() {
        return mAppBridgeBase;
    }













    

    public String getCurrentProcessName(Context context) {
        int pid = android.os.Process.myPid();
        String processName = "";
        ActivityManager manager = (ActivityManager) context.getApplicationContext().getSystemService
                (Context.ACTIVITY_SERVICE);
        for (ActivityManager.RunningAppProcessInfo process : manager.getRunningAppProcesses()) {
            if (process.pid == pid) {
                processName = process.processName;
            }
        }
        return processName;
    }

    public boolean isMainProcess(Context context) {
        

        boolean isMainProcess;
        isMainProcess = context.getApplicationContext().getPackageName().equals
                (getCurrentProcessName(context));
        return isMainProcess;
    }
}